﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ThaireInvestmentWeb.Util;
using ThaireInvestmentWeb.WcfSerivce;

namespace ThaireInvestmentWeb.Models
{
    public class EditEquityPrivilegeModel
    {
        public EquityHeader Header { get; set; }
        public EquityHeaderDTO HeaderDTO { get; set; }
        public EquityPrivilege Privilege { get; set; }
        public EditEquityPrivilegeModel()
        {
            Init();
        }
        void Init()
        {
            if (null == Header)
                Header = new EquityHeader();
            if (null == Privilege)
                Privilege = new EquityPrivilege();
            var defaultItem = WebUtil.SelectListSelectDefault();
            TypeList = new BindingList<SelectListItem>() { defaultItem };
            TypeOfPaymentList = new BindingList<SelectListItem> { defaultItem };
        }

        public static EditEquityPrivilegeModel BuildModel(EquityPrivilegeScreenData data)
        {
            var model = new EditEquityPrivilegeModel();
            model.Header = data.Header;
            if (data.Privilege != null)
            {
                model.Privilege = data.Privilege;
                model.UpdateMode = true;
            }

            model.HeaderDTO = new EquityHeaderDTO { Header = data.Header, InvolvedPartyType = data.InvolvedType };

            //type
            model.TypeList.Add(item: new SelectListItem() { Selected = false, Text = "XI", Value = "1" });
            model.TypeList.Add(item: new SelectListItem() { Selected = false, Text = "XD", Value = "2" });

            
            if (data.PaymentTypes != null || data.PaymentTypes.Any())
            {
                foreach (var selectListItem in data.PaymentTypes)
                {
                    model.TypeOfPaymentList.Add(item: new SelectListItem() { Selected = false, Text = selectListItem.PaymentTypeName, Value = selectListItem.PaymentTypeId.ToString() });
                }
            }
            model.HeaderID = model.Header.EquityHeaderID;
            if (model.Privilege != null)
                model.PrivilegeID = model.Privilege.EquityPrivilegeID;

            return model;
        }

        #region dropdownlist data
        public int? SelectedType
        {
            get { return Privilege.PrivilegeTypeID; }
            set { Privilege.PrivilegeTypeID = value; }
        }
        public IList<SelectListItem> TypeList { get; set; }

        public string SelectedTypeOfPayment
        {
            //get { return Privilege.TypeofpaymentName; }
            //set { Privilege.TypeofpaymentName = value; }
            get;
            set;
        }
        public IList<SelectListItem> TypeOfPaymentList { get; set; }

        #endregion


        #region adapter data
        public int HeaderID
        {
            get
            {
                return Privilege.EquityHeaderID.GetValueOrDefault();
            }

            set
            {
                Privilege.EquityHeaderID = value;
            }
        }
        public int? PrivilegeID
        {
            get
            {
                return Privilege.EquityPrivilegeID;
            }
            set
            {
                Privilege.EquityPrivilegeID = value.GetValueOrDefault();
            }
        }

        [Required(ErrorMessage = "โปรดระบุ")]
        [DataType(DataType.Date)]
        [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd/MM/yyyy}")]
        [Display(Name = "Ex Date")]
        public DateTime? EXDate
        {
            get { return Privilege.EXDate; }
            set { Privilege.EXDate = value; }
        }

        #endregion


        public String SubmitBtn { get; set; }
        public String CancelBtn { get; set; }
        public bool UpdateMode { get; set; }
    }
}